import { Component } from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppUrl} from "../../../app/app.general";
import {HttpClient} from "@angular/common/http";
import {mergeDeep} from "@ngx-translate/core/src/util";

/**
 * Generated class for the BusinessFoodClassifyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-business-food-classify',
  templateUrl: 'business-food-classify.html',
})
export class BusinessFoodClassifyPage {

  constructor(public navCtrl: NavController, public navParams: NavParams,public alerCtrl: AlertController,private http:HttpClient) {
    // localStorage.getItem('userid')
   this.getData();
  }

  getData() {
    this.http.post(AppUrl.foodType.selectByRestID,{restId: localStorage.getItem('restaurantid')}).subscribe(data => {
      let temp:any = data;
      if(temp.code == 200){
       this.classifyList = temp.data;
      }
    });
  }
  classifyList =[
    // {id:1,name:'凉菜'},
    // {id:2,name:'小炒'},
    // {id:3,name:'汤'},
    // {id:3,name:'汤'},
    // {id:3,name:'汤'},
    // {id:3,name:'汤'},
  ];
  ionViewDidLoad() {
    console.log('ionViewDidLoad BusinessFoodClassifyPage');
  }

  doConfirm(id,name,belong) {
    let confirm = this.alerCtrl.create({
      title: '删除菜品分类?',
      message: '请确认是否删除该分类?',
      buttons: [
        {
          text: '不同意',
          handler: () => {
          }
        },
        {
          text: '同意',
          handler: () => {
            this.http.post(AppUrl.foodType.updateByPreID,{id:id,foodtype:name,belong:localStorage.getItem('restaurantid'),del:1}).subscribe(data => {
              this.getData()
            });
          }
        }
      ]
    });
    confirm.present()
  }

  addClassify() {
    let prompt = this.alerCtrl.create({
      title: '添加菜品分类',
      message: "请输入您需要添加的菜品分类名称",
      inputs: [
        {
          name: 'typeName',
          placeholder: '分类名称'
        },
      ],
      buttons: [
        {
          text: '取消',
          handler: data => {
          }
        },
        {
          text: '保存',
          handler: data => {
            if(data.typeName.trim() == '' || data.typeName.trim() == null){
              this.showAlert("请输入菜品分类名称");
              return;
            }
            this.http.post(AppUrl.foodType.insert,{foodtype:data.typeName,belong:localStorage.getItem('restaurantid'),del:0}).subscribe(data => {
              this.getData()
            });
          }
        }
      ]
    });
    prompt.present();
  }

  showAlert(message) {
    let alert = this.alerCtrl.create({
      title: '警告',
      subTitle: message,
      buttons: ['确定']
    });
    alert.present();
  }

  modifyClassify(id,name,belong) {
    let prompt = this.alerCtrl.create({
      title: '修改菜品分类',
      message: "请输入正确的菜品分类名称",
      inputs: [
        {
          name: 'typeName',
          placeholder: name
        },
      ],
      buttons: [
        {
          text: '取消',
          handler: data => {
          }
        },
        {
          text: '保存',
          handler: data => {
            if(data.typeName == name){
              this.showAlert("菜品分类无变化！");
            }
            else if(data.typeName.trim() == '' || data.typeName.trim() == null){
              this.showAlert("请输入菜品分类名称");
            }
            else {
              this.http.post(AppUrl.foodType.updateByPreID,{id:id,foodtype:data.typeName,belong:localStorage.getItem('restaurantid'),del:0}).subscribe(data => {
                this.getData()
              });
            }
          }
        }
      ]
    });
    prompt.present();
  }
}
